clear all
set more off
set matsize 2000

*input folder path   
global filepath "D:\Dropbox\NREGA aggregate\data cleaning\replication files"

	  
cd "$filepath"
use "district deposit panel.dta", clear

local pre "yrqtr#c.wage yrqtr#c.outputwage yrqtr#c.state_frac"

*******************************************
*** Annual Deposits and Other Variables ***
*******************************************
bysort year sno: egen yr_dep = mean(dep)
bysort year sno: gen dt = _n


sum dt
keep if dt==1

replace yr_dep = yr_dep/100
reghdfe yr_dep nregs, absorb(sno year#st) cluster(sno)

egen std_ydep = std(yr_dep)

gen wave1=nr06
gen wave2=nr07
gen wave3=nr08

tab year, gen(yr)


*****************************
*** Fig. 1b: Mean by Wave ***
*****************************
bysort year: egen yr_dep_06 = mean(yr_dep) if year>=2000 & nr06==1
bysort year: egen yr_dep_07 = mean(yr_dep) if year>=2000 & nr07==1
bysort year: egen yr_dep_08 = mean(yr_dep) if year>=2000 & nr08==1

label var yr_dep_06 "Wave 1"
label var yr_dep_07 "Wave 2"
label var yr_dep_08 "Wave 3"

twoway (scatter yr_dep_06 year) (scatter yr_dep_07 year) (scatter yr_dep_08 year), xtitle(Year) ytitle(District Deposits (Annual in 100M Rs)) ylabel(0(200)1000) xline(2006)



******************************************
*** Fig. 3a: Event Figure, Early Waves ***
******************************************

forvalues i=1/10{
gen w`i'=0 if wave1~=.
	replace w`i'=1 if wave1==1 & yr`i'==1
}

*omit 2005
replace w2=0 if wave1~=.


forvalues i=1/10{
gen x`i'=0 if wave2~=.
	replace x`i'=1 if wave2==1 & yr`i'==1
}

*omit 2005
replace x2=0 if wave2~=.

*estimation
reghdfe std_ydep   w1-w10 x1-x10   , absorb(sno year#st) cluster(sno)

*store
estadd ysumm
eststo deposits_w1

drop w1-w10 x1-x10


forvalues i=1/10{
gen w`i'=0 if wave2~=.
	replace w`i'=1 if wave2==1 & yr`i'==1
}

*omit 2005q4
replace w2=0 if wave2~=.


forvalues i=1/10{
gen x`i'=0 if wave1~=.
	replace x`i'=1 if wave1==1 & yr`i'==1
}

*omit 2005q4
replace x2=0 if wave1~=.

*estimation
reghdfe std_ydep   w1-w10 x1-x10   , absorb(sno year#st) cluster(sno)

*store
estadd ysumm
eststo deposits_w2

forvalue i=1/10{
local j = 2003+`i'
label var w`i' "`j'"
}


set scheme s1mono
coefplot (deposits_w1, label("Wave 1")) (deposits_w2, label("Wave 2")), keep(w*) vertical ytitle(Coefficient on Wave*Year) ylabel(-1(.25)1)  yline(0) levels(95) ciopts(lpattern(dash)) omitted baselevels ///
     groups(w1 w2 = `""{bf:Pre-Rollout}" "(No Districts)""'        ///
            w3 w4 w5 = `""{bf:Rollout}" "(Early Districts)""'                      ///
             w6 w7 w8 w9 w10   = `""{bf:Post-Rollout}" "(All Districts)""')
			 
			 
			 
			 
			 
****************************************			 
*** Fig. 3b: Event Figure, Late Wave ***
****************************************
drop w1-w10

forvalues i=1/10{
gen w`i'=0 if wave3~=.
	replace w`i'=1 if wave3==1 & yr`i'==1
}

*omit 2005
replace w2=0 if wave3~=.

*estimation
reghdfe std_ydep   w1-w10    , absorb(sno year#st) cluster(sno)

*store
estadd ysumm
eststo deposits_w3

forvalue i=1/10{
local j = 2003+`i'
label var w`i' "`j'"
}

coefplot (deposits_w3, label("Wave 3")) , keep(w*) vertical ytitle(Coefficient on Wave*Year) ylabel(-1(.25)1) yline(0) levels(95) ciopts(lpattern(dash)) omitted baselevels ///
     groups(w1 w2 = `""{bf:Pre-Rollout}" "(No Districts)""'        ///
            w3 w4 w5 = `""{bf:Rollout}" "(Early Dist.)""'                      ///
            w6 w7 w8 w9 w10   = `""{bf:Post-Rollout}" "(All Districts)""')
			
			
			
			
			
			
/******************************************************
*** Appendix Figures: Goodman-Bacon Decompositions ***
******************************************************
use "district deposit panel.dta", clear

*balance panel
bysort sno: egen ct = count(nregs)
sum ct, d
drop if ct!=`r(max)'

*set panel
xtset sno yrqtr



*App Fig. 3: GB Decomposition, All Waves
bacondecomp std_dep nregs, ddetail


*App. Fig. 4a: GB Decomposition: 2006 & 2007
preserve 
drop if nr08==1
bacondecomp std_dep nregs, ddetail
restore

**App. Fig. 4b: GB Decomposition: 2007 & 2008
preserve 
drop if nr06==1
bacondecomp std_dep nregs, ddetail
restore

*App. Fig. 4c: GB Decomposition: 2006 & 2008
preserve 
drop if nr07==1
bacondecomp std_dep nregs, ddetail
restore
